package com.statistics.service.Impl;

import com.statistics.cloudpower.dao.*;
import com.statistics.cloudpower.entity.TblEquipment;
import com.statistics.service.EnvironmentTowerService;
import com.statistics.thread.EnvironmentStatisticsThread;
import com.statistics.thread.EnvironmentStatisticsWeekThread;
import com.statistics.thread.EnvironmentTowerStatisticsThread;
import com.statistics.thread.EnvironmentTowerStatisticsWeekThread;
import com.statistics.tools.Constants;
import com.statistics.yfdata.dao.DataEnvironmentRepository;
import com.statistics.yfdata.dao.DataEnvironmentTowerRepository;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author MeowParse
 * @date 2017/9/2.
 */
@Service
public class EnvironmentTowerServiceImpl implements EnvironmentTowerService{

	private int day_size = 30;
	private int week_size = 30;
	private int month_size = 30;

	private final org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private TblEquipmentDAO tblEquipmentDAO;
	@Autowired
	private TblEnvironmentTowerStatisticsDayDao tblEnvironmentTowerStatisticsDayDao;
	@Autowired
	private TblEnvironmentTowerStatisticsWeekDao tblEnvironmentTowerStatisticsWeekDao;
	@Autowired
	private TblEnvironmentTowerStatisticsMonthDao tblEnvironmentTowerStatisticsMonthDao;
	@Autowired
	private DataEnvironmentTowerRepository dataEnvironmentTowerRepository;

	@Override
	public void countEnvTowEveryDay() {
		Long equipment_count = tblEquipmentDAO.count(Constants.ENVIRONMENTTOWER_TYPE);
		String uuid = UUID.randomUUID().toString();
		int fragment_number = 1;
		logger.info("共有{}个环境塔设备",equipment_count);

		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE,  - 1);
		String month = String.format("%02d", cal.get(Calendar.MONTH) + 1);
		for (long start = 0; start < equipment_count; start += day_size, fragment_number++) {
			//读取count个设备
			List<TblEquipment> tblEquipment = tblEquipmentDAO.findPageBySql(Constants.ELECTRICITY_TYPE, start, day_size);
			//command为任务模式-每日、每周、每月
			new EnvironmentTowerStatisticsThread(Constants.EVERY_DAY, tblEquipment, tblEnvironmentTowerStatisticsDayDao, uuid, fragment_number,
					dataEnvironmentTowerRepository, month).start();
		}
	}

	@Override
	public void countEnvTowEveryWeek() {
		Long equipment_count = tblEquipmentDAO.count(Constants.ENVIRONMENTTOWER_TYPE);
		String uuid = UUID.randomUUID().toString();
		int fragment_number = 1;
		logger.info("共有{}个环境塔设备",equipment_count);

		for (long start = 0; start < equipment_count; start += week_size, fragment_number++) {

			//读取count个设备
			List<TblEquipment> tblEquipment = tblEquipmentDAO.findPageBySql(Constants.ELECTRICITY_TYPE, start, week_size);

			//command为任务模式-每日、每周、每月
			new EnvironmentTowerStatisticsWeekThread(tblEquipment,
					tblEnvironmentTowerStatisticsWeekDao, uuid, fragment_number,
					dataEnvironmentTowerRepository).start();
		}
	}

	@Override
	public void countEnvTowEveryMonth() {
		Long equipment_count = tblEquipmentDAO.count(Constants.ENVIRONMENTTOWER_TYPE);
		String uuid = UUID.randomUUID().toString();
		int fragment_number = 1;
		logger.info("共有{}个环境塔设备",equipment_count);

		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, -1);
		String month = String.format("%02d", (cal.get(Calendar.MONTH) + 1));
		for (long start = 0; start < equipment_count; start += month_size, fragment_number++) {

			//读取count个设备
			List<TblEquipment> tblEquipment = tblEquipmentDAO.findPageBySql(Constants.ELECTRICITY_TYPE, start, month_size);

			//command为任务模式-每日、每周、每月
			new EnvironmentTowerStatisticsThread(Constants.EVERY_MONTH,
					tblEquipment,
					tblEnvironmentTowerStatisticsMonthDao, uuid, fragment_number,
					dataEnvironmentTowerRepository, month).start();
		}
	}

}
